// import Cookies from "js-cookie";
// import { encrypt, decrypt } from "@/utils/jsencrypt";
import { setStore } from "@/utils/store"
import { encryptData } from '@/assets/js/encrypt';

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        loginName: "",
        password: "",
        // loginName: "minihu",
        // password: "EYJ17qq#",
        // loginName: "admin",
        // password: "123456",
      },
      loginRules: {
        loginName: [
          { required: true, trigger: "blur", message: "required" }
        ],
        password: [
          { required: true, trigger: "blur", message: "required" }
        ]
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    handlePwd() {
      let form = Object.assign({}, this.loginForm, {
        password: encryptData(this.loginForm.password),
      }, {
        "captchaCode": "1",
        "captchaUuid": "90cdf4372a1b431eaf5f10ff6cda327b",
        "loginDevice": 1
      });
      this.$store
        .dispatch("pwdLogin", form)
        .then(() => {
          setStore({
            type: 1,
            name: "userName",
            content: this.loginForm.username
          })
          this.$nextTick(() => {
            this.$router.push({ path: "/" })
          })
        })
        .catch(() => {
          this.loading = false;
        });
    },
    handleLogin() {
      this.loading = true
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.handlePwd()
        }
      });
    }
  }
}